<template lang="pug">
.category
  .category-title 分类编辑
  .category-body
    .category-body-header
      .category-body-title 分类标题
      .category-body-info 最多可以添加7个
    .category-list
      .cate-tag-recommend
        label.button-new-tag 推荐
      el-tag(
        v-for="tag, index in list"
        :key="tag.articleCategoryId"
        :closable="!tag.inputVisible"
        @close="handleClose(tag, index)"
      )
        el-input(
          class="input-cate-tag"
          v-if="tag.inputVisible"
          v-model="tag.categoryName"
          :ref="`saveTagInput_${tag.articleCategoryId}`"
          size="small"
          maxlength="4"
          show-word-limit
          placeholder="请输入分类标题"
          @keyup.enter.native="tagInputConfirm(tag, index)"
          @blur="tagInputConfirm(tag, index)"
        )
        label.button-new-tag(v-else @click="editCate(tag)") {{tag.categoryName}}
      el-input(
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        placeholder="请输入分类标题"
        maxlength="4"
        show-word-limit
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      )
      el-button.button-new-tag(v-else size="small" @click="showInput") + 添加
</template>

<script>
import { getArticleCategoryList, addOrUpdateCatergory, postDeleteCatergory } from 'api'
export default {
  name: 'teacherCategory',
  data () {
    return {
      list: [],
      inputVisible: false,
      inputValue: '',
      currentItem: null,
      copyList: []
    }
  },
  methods: {
    editCate (item) {
      item.inputVisible = true
      this.currentItem = item
      this.$nextTick(_ => {
        this.$refs[`saveTagInput_${item.articleCategoryId}`][0].$refs.input.focus()
      })
    },
    async getList () {
      try {
        const { ResultSet } = await this.$axios.post(getArticleCategoryList)
        if (ResultSet.userData.data.length) {
          ResultSet.userData.data.forEach(item => {
            item.inputVisible = false
          })
        }
        this.list = ResultSet.userData.data
        this.copyList = JSON.parse(JSON.stringify(ResultSet.userData.data))
      } catch (err) {
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    handleClose(tag, index) {
      this.$confirm('是否删除该分类？', '提示').then(async () => {
        try {
          await this.$axios.post(postDeleteCatergory, { articleCategoryId: tag.articleCategoryId })
          this.list.splice(index, 1)
          this.$message.success('删除成功！')
        } catch (err) {
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    async changeCate (name) {
      try {
        let query = { categoryName: name }
        if (this.currentItem && this.currentItem.articleCategoryId) query.articleCategoryId = this.currentItem.articleCategoryId
        await this.$axios.post(addOrUpdateCatergory, { ...query })
        if (this.currentItem && this.currentItem.articleCategoryId) {
          this.$message.success('修改成功！')
          this.currentItem = null
        } else {
          this.$message.success('添加成功！')
        }
        this.getList()
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async handleInputConfirm () {
      if (this.list.length >= 6) {
        this.$message.error('文章分类最多可以添加7个！')
        this.inputVisible = false
        this.inputValue = ''
        return
      }
      let inputValue = this.inputValue
      if (inputValue) {
        await this.changeCate(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    async tagInputConfirm (item) {
      let row = this.copyList.find(row => row.articleCategoryId === item.articleCategoryId)
      if (!item.categoryName) {
        item.categoryName = row.categoryName
        item.inputVisible = false
        // this.$set(item, 'categoryName', row.categoryName)
      } else {
        if (row.categoryName !== item.categoryName && item.categoryName) {
          await this.changeCate(item.categoryName)
        }
        item.inputVisible = false
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  }
}
</script>

<style lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
  cursor: pointer;
}
.input-cate-tag {
  width: 150px;
  .el-input__inner {
    border: none;
    height: 30px;
    line-height: 30px;
    background: transparent;
  }
}
.input-new-tag {
  width: 150px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

<style lang="scss" scoped>
.category {
  width: 100%;
  height: 100%;
  padding: 15px 32px;
}
.category-title {
  font-size: 16px;
  line-height: 22px;
  color: #363636;
}
.category-body {
  background: #fff;
  border-radius: 4px;
  widows: 100%;
  height: 729px;
  margin-top: 20px;
  padding: 25px 32px;
}
.category-body-header {
  display: flex;
  align-items: center;
  .category-body-title {
    font-size: 14px;
    line-height: 20px;
    color: #363636;
    font-weight: bold;
  }
  .category-body-info {
    font-size: 14px;
    line-height: 20px;
    color: #bbb;
    margin-left: 10px;
  }
}
.category-list {
  margin-top: 30px;
}
.cate-tag-recommend {
  background-color: #fff1f3;
  border-color: #ffe2e8;
  display: inline-block;
  height: 32px;
  padding: 0 10px;
  line-height: 30px;
  font-size: 12px;
  color: #FF708B;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  box-sizing: border-box;
  white-space: nowrap;
  margin-right: 10px;
  .button-new-tag {
    margin-left: 0;
  }
}
</style>